from __future__ import with_statement

from django.http import HttpResponse

import time
import thread
import threading
import os

def stream(request):
    def go():
        i = 0
        while True:
            yield ('[THREAD: %d] => %s%s sending... %d<br/>' % (
                thread.get_ident(), 
                request.path, 
                '?%s' % request.META['QUERY_STRING'] 
                if request.META['QUERY_STRING'] else '', 
                i,
            ))
            i += 1
            time.sleep(1)
            
    return HttpResponse(go())
 
def s10(request):
    def go():
        i = 0
        while i < 10:
            yield ('[THREAD: %d] => %s%s sending... %d<br/>' % (
                thread.get_ident(), 
                request.path, 
                '?%s' % request.META['QUERY_STRING'] 
                if request.META['QUERY_STRING'] else '', 
                i,
            ))
            i += 1
            time.sleep(1)
            
    return HttpResponse(go())
    
def threads(request):
    threading.currentThread().setName('threads')
    return HttpResponse('%d<br/>%s<br/>' % 
        (len(threading.enumerate()) , str(threading.enumerate())))
   
def large(request):
    p = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'large.png')
    with open(p, "rb") as f:
        d = f.read()
    return HttpResponse(d, content_type='image/png')
